class MasterSlaveDBRouter(object):
    """ 数据库读写分离的路由"""

    def db_for_read(self, model, **hints):
        """ 读数据库　select"""
        return "slave"

    def db_for_write(self, model, **hints):
        """"写数据库 update insert delete"""
        return "default"

    def allow_relation(self, obj1, obj2, **hints):
        """是否运行关联工作"""
        return True


# def db_for_read(self, model, **hints):
#     """ 读数据库　select"""
#     # if model._meta.app_label == 'app02':  # app name（如果该app不存在，则无法同步成功）
#     #     return 'hvdb'  # hvdb为settings中配置的database节点名称，并非db name。dbname为
#     return "slave"
# class mtable01(models.Model):
#     name=models.CharField(max_length=100,primary_key=True,unique=True)
#     ip=models.GenericIPAddressField()
#     rating = models.IntegerField()
#
#     def __str__(self):
#         return self.name
#
#     class Meta:
#         app_label = 'app02' #定义该model的app_label
#         ordering = ['name'] 使用migrate命令同步数据库：